package com.hqyj.module.login.service.impl;

import com.hqyj.module.login.dao.UserInfoMapperDao;
import com.hqyj.module.login.dao.YanUserMapper;
import com.hqyj.module.login.pojo.UserInfo;
import com.hqyj.module.login.pojo.UserInfoNew;
import com.hqyj.module.login.service.IUserInfoService;
import com.hqyj.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class IUServiceImpl implements IUserInfoService {
    @Autowired(required = false)
    private UserInfoMapperDao userInfoMapper;

    @Autowired
    YanUserMapper yanUserMapper;

    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Autowired
    EmailUtil emailUtil;

    @Override
    public HashMap<String, Object> login(UserInfo userInfo) {
        HashMap<String,Object> map = new HashMap<>();
        UserInfo login = userInfoMapper.login(userInfo);
        if(login == null){
            map.put("info","登录失败");
            map.put("userId",login.getUserId());
            return map;
        }else {
            map.put("info","登录成功");
            map.put("userId",login.getUserId());
            return map;
        }
    }


    @Override
    public HashMap<String, Object> emailSend(String email, HttpServletRequest request) {
        //生成4位随机数
        Random random = new Random();
        int code = random.nextInt(9000)+1000;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String format = df.format(new Date());// new Date()为获取当前系统时间
        UserInfo userInfo = new UserInfo(email,String.valueOf(code),format);
        HashMap<String, Object> map = emailUtil.sendEmail(email, "你的随机验证码是：" + code, "xxx公司登录验证码");
        HttpSession session = request.getSession();
        if(map.get("info").equals("发送成功")){
            session.setAttribute("code",code);
        }
        int i = userInfoMapper.regiestUserEmail(userInfo);
        return map;
    }

    @Override
    public HashMap<String, Object> emailLogin(UserInfo userInfo) {
        UserInfo userInfo1 = userInfoMapper.emailLogin(userInfo);
        HashMap<String,Object> map = new HashMap<>();
        if(userInfo1 != null){
            map.put("info","登录成功");
        }else {
            map.put("info","登录失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> emailRedis(String email) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //生成4位随机
        Random rd = new Random();
        int code = rd.nextInt(9000)+10000;
        map=emailUtil.sendEmail(email,"你的验证码是："+code,"xxxx公司的验证码");
        if(map.get("info").equals("发送成功")){
            //存入redis
            redisTemplate.opsForValue().set(email,code+"",3, TimeUnit.MINUTES);
        }
        return map;
    }

    @Override
    public HashMap<String, Object> emailLoginRedis(UserInfo userInfo) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        String code1 = redisTemplate.opsForValue().get(userInfo.getUserName())+"";
        if(userInfo.getUserPwd().equals(code1)){
            map.put("info","邮箱登录成功");
        }else{
            map.put("info","验证码输入不正确");
        }
        return map;
    }
    
    @Override
    public Integer updatePwd(UserInfoNew userInfoNew) {
        return userInfoMapper.updatePwd(userInfoNew);
    }
}
